import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 一个对象就是一条路由规则,引入多个动态路由模块
import departments from './modules/departments'
import employees from './modules/employees'
import approvals from './modules/approvals'
import settings from './modules/settings'
import salarys from './modules/salarys'
import social_securitys from './modules/social_securitys'
import permissions from './modules/permissions'
import attendances from './modules/attendances'

// 动态路由表，项目中不同的用户可以访问不同的功能
// synchronous 同步     asynchronous 异步
// 组合动态路由模块
export const asyncRoutes = [
  departments, // 公司的人员架构
  settings, // 角色管理
  employees, // 给员工分配角色
  permissions, // 配置各种权限点
  approvals, // 本期不实现功能
  salarys, // 本期不实现功能
  social_securitys, // 本期不实现功能
  attendances // 本期不实现功能
]

// 静态路由表，项目中每个用户都可以访问的功能
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    // 显示与隐藏  true 隐藏   false 显示
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'bank' }
      }
    ]
  },
  // 不能设置在静态路由页面
  // {
  //   path: '/employees/import',
  //   component: Layout,
  //   children: [
  //     {
  //       path: '',
  //       name: 'Import',
  //       component: () => import('@/views/employees/empImport.vue'),
  //       hidden: true, // 不显示到左侧菜单
  //       meta: { title: 'Excel 导入', icon: 'people' }
  //     }
  //   ]
  // },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // 控制路由滚动行为  滚动到顶部
    scrollBehavior: () => ({ y: 0 }),
    // 组合到一起组成路由表
    // routes: [...constantRoutes, ...asyncRoutes]
    routes: [...constantRoutes]
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
